//
//  WXCloudNewsListVideoView.m
//  xinguiyang
//
//  Created by 段尹斌 on 2017/12/7.
//  Copyright © 2017年 voc. All rights reserved.
//

#import "WXCloudNewsListVideoView.h"
#import <WXCloudModuleCommonNewList/WXFunctionEnterCollectionView.h>
//#import "WXFunctionEnterCollectionView.h"
#import <Masonry/Masonry.h>
#import <WXCloudModuleCategories/UIView+LayoutMethods.h>
#import <WXCloudModuleBaseConfig/WXSystemBaseDefine.h>
#import <WXCloudModuleBaseView/WXCommonNewsViewMediatorHeightManager.h>
@interface WXCloudNewsListVideoView ()<WXCycleScrollViewDelegate,WXNewsBaseTableViewDelegate,UICollectionViewDelegate>
/** 功能入口视图 */
@property (nonatomic, strong, readwrite)WXFunctionEnterCollectionView *enterCollectionView;
/** 新闻列表视图 */
@property (nonatomic, strong, readwrite) WXNewsBaseTableView *tableView;
/** 轮播图 */
@property (nonatomic, strong, readwrite) WXCycleScrollView *cycleScrollView;
/** ViewModel */
@property (nonatomic, strong, readwrite) WXCloudNewsListVideoViewModel *viewModel;
/** TableView头部视图 */
@property (nonatomic, strong) UIView *topHeaderView;

/**
 *  Frame参数
 */
/** 轮播图高度 */
@property (nonatomic, assign, readonly) CGFloat cycleViewHeight;
/** 功能入口高度 */
@property (nonatomic, assign, readonly) CGFloat enterCollectionHeight;

/**
 *  布尔条件参数
 */
/** 是否存在轮播图 */
@property (nonatomic, assign) BOOL isHaveCycleView;
/** 是否存在功能入口 */
@property (nonatomic, assign) BOOL isHaveEnterCollectionView;

@end


@implementation WXCloudNewsListVideoView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

#pragma mark -公开方法
- (void)bindingTo:(WXCloudNewsListVideoViewModel *)viewModel
{
    _viewModel = viewModel;
    [self addReactiveObserve];
}
#pragma mark -开启自动轮播器的计时器
- (void)startNewsCycleViewTimer
{
    [_cycleScrollView startCycleViewTimer];
}
#pragma mark -停止自动轮播器的计时器
- (void)stopNewsCycleViewTimer
{
    [_cycleScrollView stopCycleViewTimer];
}

#pragma mark -私有方法
- (void)initialize
{
    self.isHaveCycleView = NO;

    [self.topHeaderView addSubview:self.cycleScrollView];
    [self.topHeaderView addSubview:self.enterCollectionView];
    self.tableView.tableHeaderView = self.topHeaderView;
    [self addSubview:self.tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self);
    }];
}

- (void)addReactiveObserve
{
    _tableView.viewModelDelegate = self.viewModel;
    @weakify(self)
    /** 监听新闻里面的轮播图模型数组 */
    [RACObserve(_viewModel, cycleViewModelList) subscribeNext:^(NSArray *array) {
        @strongify(self)
        self.cycleScrollView.hidden = !array.count;
        self.isHaveCycleView = array.count;
        if ([array isKindOfClass:[NSArray class]] && array.count>0) {
            self.cycleScrollView.modelsGroup = array;
        } else {
            self.cycleScrollView.modelsGroup = nil;
        }
        [self updateTopHeaderViewHeight];
    }];
    
    [RACObserve(_viewModel, functionModelList) subscribeNext:^(NSArray *array) {
        @strongify(self)
        self.enterCollectionView.hidden = !array.count;
        self.isHaveEnterCollectionView = array.count;
        if ([array isKindOfClass:[NSArray class]] && array.count>0) {
            self.enterCollectionView.functionEnterList=array;
        } else {
            self.enterCollectionView.functionEnterList = nil;
        }
        [self updateTopHeaderViewHeight];
        [self.enterCollectionView reloadData];
    }];
    
    /** 监听新闻里面的新闻模型数组 */
    [[RACObserve(_viewModel, homeNewsModelList) ignore:nil] subscribeNext:^(NSArray *array) {
        @strongify(self)
        [self.tableView reloadDataArray:array];
        [self updateTopHeaderViewHeight];
    }];
    
    [[RACObserve(_viewModel, isNoMoreDataList) ignore:nil] subscribeNext:^(NSNumber *value) {
        @strongify(self)
        [self.tableView endRefreshingWithRequestBool:!value.boolValue];
    }];
}

- (void)updateTopHeaderViewHeight
{
    if (self.isHaveCycleView == YES && self.isHaveEnterCollectionView == YES) {
        self.topHeaderView.height = self.cycleViewHeight+self.enterCollectionHeight+5;
        self.cycleScrollView.y = 0;
        self.cycleScrollView.height = self.cycleViewHeight;
        self.enterCollectionView.y = self.cycleViewHeight+5;
        self.enterCollectionView.height = self.enterCollectionHeight;
    } else if (self.isHaveCycleView == YES && self.isHaveEnterCollectionView == NO) {
        self.topHeaderView.height = self.cycleViewHeight;
        self.cycleScrollView.y = 0;
        self.cycleScrollView.height = self.cycleViewHeight;
        self.enterCollectionView.y = 0;
        self.enterCollectionView.height = 0;
    } else if (self.isHaveCycleView == NO && self.isHaveEnterCollectionView == YES) {
        self.topHeaderView.height = self.enterCollectionHeight;
        self.cycleScrollView.y = 0;
        self.cycleScrollView.height = 0;
        self.enterCollectionView.y = 0;
        self.enterCollectionView.height = self.enterCollectionHeight;
    } else {
        self.topHeaderView.height = 0;
        self.cycleScrollView.y = 0;
        self.cycleScrollView.height = 0;
        self.enterCollectionView.y = 0;
        self.enterCollectionView.height = 0;
    }
    self.tableView.tableHeaderView = self.topHeaderView;
    [self.tableView reloadData];
}


#pragma mark -懒加载
- (WXNewsBaseTableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[WXNewsBaseTableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.viewModelDelegate = self.viewModel;
        _tableView.customDelegate = self;
        _tableView.scrollsToTop = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

- (WXCycleScrollView *)cycleScrollView
{
    if (_cycleScrollView == nil) {
        CGFloat maxY = 0;
        _cycleScrollView = [WXCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, maxY, WXScreenWidth,self.cycleViewHeight) scrollStyle:WXcycleScrollViewAnimationStyleClassic delegate:self placeholderImage:[UIImage imageNamed:@"zhuantiplaceholder"]];
    }
    return _cycleScrollView;
}
- (UIView *)topHeaderView
{
    if (_topHeaderView == nil) {
        CGFloat topHeaderViewHeight = CGRectGetMaxY(self.cycleScrollView.frame);
        _topHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0,
                                                                 kWXNavigationBarHeight,
                                                                 WXScreenWidth,
                                                                 topHeaderViewHeight)];
    }
    return _topHeaderView;
}

- (CGFloat)cycleViewHeight
{
    CGFloat tempHeight = [WXCommonNewsViewMediatorHeightManager sharedInstance].cycleViewHeight;
    return tempHeight;
}

- (CGFloat)enterCollectionHeight
{
    NSLog(@"WXHeightScale(80)==%f",WXHeight(80));
    return WXHeight(80);
}
@end

